/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package messageBean;

import entities.OrderEntity;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.EJB;
import javax.ejb.MessageDriven;
import javax.ejb.MessageDrivenContext;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import sessionBeans.OrderEntityFacade;

/**
 *
 * @author HOANG ANH
 */
@MessageDriven(mappedName = "jms/OrderMessage", activationConfig =  {
        @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
        @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")
    })
public class OrederMessageBean implements MessageListener {
    @EJB
    private OrderEntityFacade orderEntityFacade;
    @Resource
    private MessageDrivenContext mdc;
    
    public OrederMessageBean() {
    }

    public void onMessage(Message message) {
        ObjectMessage msg = null;
        OrderEntity order = null;
        if (message instanceof ObjectMessage) {
            try {
                msg = (ObjectMessage) message;
                order = (OrderEntity) msg.getObject();
                orderEntityFacade.saveNew(order);
            } catch (JMSException ex) {
                ex.printStackTrace();
                mdc.setRollbackOnly();
            }
        }
    }
    
}
